package com.itheima.jobs;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.service.ReportService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.HashMap;
import java.util.Set;

public class RecordJob {
    @Autowired
    private JedisPool jedisPool;

    @Reference
    private ReportService reportService;

    public void recordData(){
        Jedis jedis = jedisPool.getResource();

        Set<String> mysortedset = jedis.zrange("interfaceCount", 0, -1);
        HashMap<String, Object> map = new HashMap<>();

       if(mysortedset.size()!=0){
            for (String interfaceName : mysortedset) {
                Double zscore = jedis.zscore("interfaceCount", interfaceName);
                double v = (zscore == null) ? 1.0 : (zscore + 1.0);
                map.put(interfaceName,jedis.zscore("interfaceCount",interfaceName).doubleValue());
            }
        }

        String todayTime = null;
        Date todayDate = null;
        try {
            todayTime = DateUtils.parseDate2String(DateUtils.getToday());
            todayDate = DateUtils.parseString2Date(todayTime);
        } catch (Exception e) {
            e.printStackTrace();
        }


        map.put("date",todayDate);
        try {
            reportService.setInterfaceReportData(map);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("空指针了");
        }


        //重置set内各字段的次数
        String[] interfaceNames = {"add","del","edit","pageQuery"};
        String[] methodNames = {"item_","group_"};



        for (String methodName : methodNames) {
            for (String interfaceName : interfaceNames) {
                jedis.zadd("interfaceCount",0.0,methodName+interfaceName);
            }
        }

    }
}
